package com.xujl.mvcbind.ui.element;

import android.view.View;
import android.view.ViewStub;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.xujl.elementlib.main.BaseElement;
import com.xujl.fastlib.base.BaseRvAdapter;
import com.xujl.mvcbind.R;
import com.xujl.mvcbind.adapter.ListAdapter;
import com.xujl.mvcbind.bean.ListBean;
import com.xujl.mvcbind.databinding.ElementListBinding;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * @author x
 * @Description
 */
public class ListElement extends BaseElement<ElementListBinding> implements OnItemClickListener {
    private BaseRvAdapter<ListBean> mAdapter;
    private List<ListBean> mList;

    public ListElement (ViewStub viewStub) {
        super(viewStub);
    }

    @Override
    protected int getLayoutId () {
        return R.layout.element_list;
    }

    @Override
    public void initView () {
        super.initView();
        mList = new ArrayList<>();
        mBind.rv.setLayoutManager(new LinearLayoutManager(getRoot().getContext()));
        mAdapter = new ListAdapter(mList);
        mBind.rv.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    public void refreshData (List<ListBean> list) {
        mList.clear();
        mList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick (BaseQuickAdapter adapter, View view, int position) {
        mList.get(position).setChecked(!mList.get(position).isChecked());
        mAdapter.notifyDataSetChanged();
    }

    public String getCheckData () {
        StringBuilder result = new StringBuilder();
        for (ListBean bean : mList) {
            if (bean.isChecked()) {
                result.append(bean.getTitle());
            }
        }
        return result.toString();
    }
}
